package alg1;

import java.util.*;

/**
 * @author 24岁没牵过女人的手
 * 日期：2023/8/20 19:09
 * 文件描述：
 */
public class ALGO2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int c = in.nextInt();
        System.out.println(c);
        for (int j = 0; j < c; j++) {
            while (in.hasNextLine()) { // 注意 while 处理多个 case
                List<Integer> input = getInput(in.nextInt());
                int n = input.get(0);
                int k = input.get(1);
                int[] redPositions = new int[3];
                for (int i = 0; i < 3; i++) {
                    redPositions[i] = input.get(i + 2);
                }
                System.out.println(Arrays.toString(redPositions));
                int min = lestSwitch(n, k, redPositions);
                System.out.println(min);
            }
        }

    }

    private static List<Integer> getInput(int rawInput) {
        List<Integer> input = new ArrayList<>();
        while (rawInput > 0) {
            int num = rawInput % 10;
            input.add(num);
            rawInput /= 10;
        }
        Collections.reverse(input);
        return input;
    }

    private static int lestSwitch(int n, int k, int[] redPositions) {
        String[] chain = new String[n];
        for (int i = 0; i < n; i++) {
            if (i == redPositions[0] || i == redPositions[1] || i == redPositions[2]) {
                chain[i] = "RED";
            } else {
                chain[i] = "WHITE";
            }
        }
        int minSwitch = Integer.MAX_VALUE;
        int redCount = 3;

        for (int i = 0; i < n; i++) {
            int switches = 0;
            int red = 0;
            String[] t = new String[n];
            System.arraycopy(chain, i, t, 0, n - i);
            System.arraycopy(chain, 0, t, n - i, i);
            int left = 0;
            int right = 0;
            while (right < n && red < redCount) {
                if (t[right] == "RED") {
                    red++;
                }
                right++;
            }
            if (red < redCount) {
                return -1;
            }
            right--;

            while (right < n) {
                switches += right - left;
                if (t[left] == "RED") {
                    red--;
                }
                left++;
                right++;
                while (right < n && t[right] != "RED") {
                    right++;
                }
            }
            minSwitch = Math.min(minSwitch, switches);
        }
        return minSwitch;
    }
}
